#!/usr/bin/python
#pingxue
#coding=utf-8
import socket, ssl;
import time;
import select;
import struct;

class Socket_Req:

    def __init__(self, ip, http_port, https_port, file_name, body_name):
        self.ip = ip
        self.http_port = http_port
        self.https_port = https_port
        self.file_name = file_name 
        self.body_name = body_name 
        self.sock = 0 
        self.ssl_sock = 0 

    def get_body(self, sock):
        req = b""        
        if not self.body_name:
            return
#        file1 = open(self.file_name, "rb", encoding = "utf-8")
        file1 = open(self.body_name, "rb")
        while(1):
            line = file1.readline()
            if not line:
                break;
            line = line.strip(b'\n')
            line = line.strip(b'\r')
            req = req + line;
        #req = req + b"\r\n"
        file1.close()
        sock.sendall(req)
        sock.setblocking(0)
   
        while(1):
#        for i in range(2):
            ready = select.select([sock], [], [], 2.0)
            if ready[0]:
                resp = sock.recv(1024)
            else:
                resp=""
            if not resp:
                break
            resp_len = len(resp)
            print("receiving...")
            print(resp_len)
            if resp_len == 0:
                break
            print(resp)
#            break;



    def get_req(self, sock):
        req = b""        
#        file1 = open(self.file_name, "rb", encoding = "utf-8")
        file1 = open(self.file_name, "rb")
        while(1):
            line = file1.readline()
            if not line:
                break;
            line = line.strip(b'\n')
            line = line.strip(b'\r')
            req = req + line + b'\r\n';
        req = req + b"\r\n"
        file1.close()
        sock.sendall(req)
        sock.setblocking(0)
   
        while(1):
#        for i in range(2):
            ready = select.select([sock], [], [], 3.0)
            if ready[0]:
                resp = sock.recv(1024)
            else:
                resp=""
            if not resp:
                break
            resp_len = len(resp)
            print("receiving...")
            print(resp_len)
            if resp_len == 0:
                break
            print(resp)
#            break;

    def create_sock(self):
        s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        s.connect((self.ip, self.http_port))
        l_onoff = 1                                                                                                                                                           
        l_linger = 0                                                                                                                                                          
        s.setsockopt(socket.SOL_SOCKET, socket.SO_LINGER, struct.pack('ii', l_onoff, l_linger))
        self.sock = s

    def create_ssl(self):
        context = ssl.SSLContext(ssl.PROTOCOL_SSLv23)
        context.check_hostname = False
        context.verify_mode = ssl.CERT_NONE

        s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        ssl_sock = context.wrap_socket(s, server_hostname = self.ip)
        ssl_sock.connect((self.ip, self.https_port))
        cert = ssl_sock.getpeercert()
        self.ssl_sock = ssl_sock

    def close_sock(self):
        self.sock.close()

    def close_ssl(self):
        self.ssl_sock.close()

    def send_http(self):
        self.create_sock()
        self.get_req(self.sock)
        #time.sleep(1)
        self.get_body(self.sock)
        self.close_sock()

    def send_https(self):
        self.create_ssl()
        self.get_req(self.ssl_sock)
        self.close_ssl()






